<?php
/**
 * 
 * @author Paolo Bertuzzo
 *
 */
namespace User\Data;

use User\Entity\Customer;

class CustomerDao extends AbstractDao
{
    
    /**
     * 
     * @param string $username
     */
    public function get($username) 
    {
        $query = $this->entityManager->createQuery('select c from User\Entity\Customer c where c.username = ?1');
        
        $query->setParameter(1, $username);
        
        $result = $query->getResult();
        
        return count($result) == 0 ? null : $result[0];
    }
    
    /**
     * 
     * @param string $uid
     * @param string $username
     * @param string $firstName
     * @param string $lastName
     * @param string $email
     * @param string $providedBy
     */
    public function insert($uid, $username, $firstName, $lastName, $email, $providedBy) 
    {
    	$customer = new Customer();
    	
    	$customer->setUid($uid);
    	$customer->setUsername($username);
    	$customer->setEmail($email);
    	$customer->setFirstName($firstName);
    	$customer->setLastName($lastName);
    	$customer->setPassword(null);
    	$customer->setProvidedBy($providedBy);
        
    	$this->entityManager->persist($customer);
    	$this->entityManager->flush();
    	
    	return $customer;
    }
    
    /**
     * 
     * @param integer $customerId
     * @param string $firstName
     * @param string $lastName
     * @param string $email
     */
    public function update($customerId, $firstName, $lastName, $email)
    {
        $query = $this->entityManager->createQuery('select c from User\Entity\Customer c where c.customerId = ?1');
        
        $query->setParameter(1, $customerId);

        if (count($result) == 0)
        	$this->logger->info(sprintf('Customer not found for id %d', $customerId));
        
        $customer = $query->getResult()[0];
        
        $customer->setFirstName($firstName);
        $customer->setLastName($lastName);
        $customer->setEmail($email);
        
        $this->entityManager->merge($customer);
        $this->entityManager->flush();
         
        return $customer;
    }
    
}

?>